#include "ScreenTest.h"
#include "Map.h"

#include "PlayerStatHUD.h"
#include "Player.h"

#include "ToolManager.h"
#include "GUISystem.h"
#include "SelectTool.h"
#include "StoneTowerShopItem.h"
#include "TexturePool.h"

ScreenTest::ScreenTest(void)
{

}


ScreenTest::~ScreenTest(void)
{
	
}

void ScreenTest::draw(SDL_Renderer * renderer)
{
	SDL_SetRenderDrawColor(renderer,0,0,0,255);
	SDL_RenderClear(renderer);
	
	arena.drawArena(renderer);
	hudMgr.drawHud(renderer);
	ToolManager::getPtr()->drawTool(renderer);
	GUISystem::getPtr()->drawGUI(renderer);
	SDL_RenderPresent(renderer);
}

void ScreenTest::init(SDL_Window * window)
{
	
	GamePlayMediator::getPtr()->setArena(&arena);
	GamePlayMediator::getPtr()->setPlayer(&pl);

	arena.loadMapData("Data/BasicMap/basicMapData.txt");
	arena.loadMapTexture("Data/BasicMap/basicMap.bmp");
	arena.loadRoad("Data/BasicMap/basicMapRoad.txt");

	tower.init();
	//tower.setPosition(680,45);
	tower.setRangeValue(100);
	tower.setDamageValue(100);
	tower.setShootRate(70);
	tower.showRange(false);
	SelectTool * tool = new SelectTool();
	
	ToolManager::getPtr()->setActiveTool(tool);
	//tower.showRange(false);
	//arena.addTower(&tower);

     enemy = new EnemyBase();

	
	//enemy->loadTextureFromFile("enemy.bmp");
	enemy->setPosition(300,100);
	enemy->setSpeed(3);
	enemy->init();
	enemy->setMaxLife(200);
	enemy->setGold(100);
	//enemy->hideLifeBar();
	wave.setSpawnRate(200);

	EnemyBase * enemy2 = new EnemyBase();
	//enemy2->loadTextureFromFile("enemy.bmp");
	enemy2->setSpeed(1);
	enemy2->init();

	wave.addElement(enemy);
	wave.addElement(enemy2);
	arena.addWave(&wave);

	pl.setMaxHealth(100);
	pl.setHealth(100);
	pl.setGold(20);
	pl.setLevel(1);

	PlayerStatHUD *  hud = new PlayerStatHUD();
	hud->init();
	hud->setPosition(700,200);

	hud->setPlayer(&pl);

	pl.addObserver(hud);

	hudMgr.addElement(hud);

	shop.init();
	StoneTowerShopItem * item = new StoneTowerShopItem();
	item->init();
	shop.addShopItem(item);
	

	GUISystem::getPtr()->setGUISheet(shop.getWidget());

}

void ScreenTest::onEvent(SDL_Event & evt)
{
	/*if(evt.type == SDL_KEYDOWN)
	{
		TowerCreationTool * tct = new TowerCreationTool();
		tct->setTower(&tower);
		ToolManager::getPtr()->setActiveTool(tct);
	}*/

	if(evt.type == SDL_MOUSEBUTTONDOWN)
	{
		ev.type = MOUSE_EVENT;
		ev.mouse.type = CLICK;
		ev.mouse.x = evt.button.x;
		ev.mouse.y = evt.button.y;
		GUISystem::getPtr()->injectMousePos(ev.mouse.x,ev.mouse.y);
		GUISystem::getPtr()->injectMouseClick(ev.mouse.button);
		ToolManager::getPtr()->onEvent(ev);
	}

	
	if(evt.type == SDL_MOUSEMOTION)
	{
		ev.type = MOUSE_EVENT;
		ev.mouse.type = MOUSE_MOVE;
		ev.mouse.x = evt.button.x;
		ev.mouse.y = evt.button.y;
		ToolManager::getPtr()->onEvent(ev);
	}


}

void ScreenTest::update()
{
	arena.updateArena();
}

void ScreenTest::onClose()
{
	
	hudMgr.deleteAllElements();
	GamePlayMediator::cleanUp();
	wave.deleteAllElements();
	TexturePool::cleanUp();

}
void ScreenTest::onPause()
{

}
void ScreenTest::onRestart()
{

}

